package com.xhdx.threadpool;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * 创建一个定长线程池，支持定时及周期性任务执行
 */
public class ThreadPoolScheduledTest {
    public static void main(String[] args) {
        cron();
        cron1();
    }
    /**
     * 表示延迟4秒执行
     */
    public static void cron(){
        ScheduledExecutorService scheduledThreadPool = Executors.newScheduledThreadPool(5);
        scheduledThreadPool.schedule(new Runnable() {
            public void run() {
                System.out.println(Thread.currentThread().getName()+"正在被执行===4");
            }
        }, 4, TimeUnit.SECONDS);
    }
    /**
     * 表示延迟1秒后每4秒执行一次
     */
    public static void cron1(){
        ScheduledExecutorService scheduledThreadPool = Executors.newScheduledThreadPool(5);  
        scheduledThreadPool.scheduleAtFixedRate(new Runnable() {  
         public void run() {  
          System.out.println(Thread.currentThread().getName()+"正在被执行===1-4");  
         }  
        }, 1, 4, TimeUnit.SECONDS);  
    }
}